<?
header('content-type:text/html;charset=utf-8');
class Admin_Article_Controller extends Base_Controller {

	public function action_index()
	{
        $username = Session::get('username');
        $s= Input::get('s',null);

        $args['u.content'] = array('like', '%'.$s.'%');
        $args['u.title'] = array('like', '%'.$s.'%');
        $articles = DB::table('article_kind as o')
                -> left_join('article_content as u', 'u.kid', '=', 'o.id')
                -> where(function($query) use($args) {
                foreach($args as $column => $val)
                {
                    $query->or_where($column, $val[0], $val[1]);
                }
                })
                ->order_by('u.id', 'asc')->paginate(15);
            return View::make('admin/article',array('username'=>$username,'articles'=>$articles,'s'=>$s));

	}

	 public function action_change()
    {   
        $id = $_GET['id'];
        $username = Session::get('username');
        $str = DB::table('article_content')->where('id','=',$id)->first();
        $val = DB::table('article_kind')->get();
        return View::make('admin/articlechange',array('username'=>$username,'str'=>$str,'res2'=>$val));
       
    }


    public function action_dochange()
    {
    
    	$username = Session::get('username');
        $author = Input::get('author');
        $title = Input::get('title');
        $kid = Input::get('modules');
        $content = Input::get('content');
        $id = Input::get('id');
        $time = time();
        $res = DB::table('article_content')->where('id','=',$id)->update(array('title' =>$title,'content'=>$content,'author'=>$author,'addtime'=>$time,'kid'=>$kid));        
        if($res){
        	 return View::make('jump/error2',array('msg'=>'修改成功.....', 'time'=>2, 'url'=>'index'));
        }else{
        	return View::make('jump/error',array('msg'=>'修改失败', 'time'=>3));
        }
    }



    public function action_dele()
    {   
        $id = $_GET['id'];
        $username = Session::get('username');
        $tr = DB::table('article_content')->where('id','=',$id)->delete();
        if($tr){
            return View::make('jump/error2',array('msg'=>'删除成功.....','time'=>1,'url'=>'index'));
        }else{
            return View::make('jump/error',array('msg'=>'删除失败','time'=>3));
        }    
    }



     public function action_add()
    {   
        $username = Session::get('username');
    	$val = DB::table('article_kind')->get();
        return View::make('admin/articleadd',array('username'=>$username,'val'=>$val));
    }

    public function action_doadd()
    {
    	$username = Session::get('username');
    	$title = Input::get('title');
    	$author = Input::get('author');
    	$date = Input::get('date');
    	$addtime = strtotime($date);
    	$kid = Input::get('modules');
    	$content = Input::get('content');
    	$tr = DB::table('article_content')->insert(array('author' => $author,'title' => $title, 'addtime' => $addtime, 'kid' => $kid, 'content' => $content));
    	if($tr){
    		return View::make('jump/error2',array('msg'=>'添加文章成功.....','time'=>1,'url'=>'index'));
    	}else{
    		return View::make('jump/error',array('msg'=>'添加文章失败.....','time'=>3));
    	}  	
    }


    public function action_search()
    {

        $time1 = strtotime(Input::get('date1'));
        $time2 = strtotime(Input::get('date2'));

        $articles = DB::table('article_kind')
                     ->left_join('article_content','article_content.kid','=','article_kind.id')
                     ->where('article_content.addtime','<',$time2)
                     ->order_by('article_content.id','asc')->paginate(25);
            $count = DB::table('article_content')->count();
            $all = ceil($count/25);    
        $username = Session::get('username');
        return View::make('admin/articlesearch',array('username'=>$username,'articles'=>$articles,'all1'=>$all));


    }













}